package info.liujiachen.security.test.service;

import info.liujiachen.security.test.pojo.JwtUserDetails;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class JwtUserDetailsService implements UserDetailsService {

    private Map<String, JwtUserDetails> cache = new HashMap<>();

    @Override
    public JwtUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null || !username.equals("user")) {
            throw new UsernameNotFoundException("Not Found");
        } else {
            return new JwtUserDetails(1l, "user", "123456", AuthorityUtils.createAuthorityList("user"));
        }
    }

    public JwtUserDetails getCacheUser(String username) {
        return this.cache.get(username);
    }

    public void cacheUser(JwtUserDetails details) {
        this.cache.put(details.getUsername(), details);
    }

}
